package com.taotao.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.taotao.common.service.HttpClientService;
import com.taotao.common.service.RedisService;
import com.taotao.manage.pojo.ItemVO;

@Service
public class ItemService {
	
	@Autowired
	private HttpClientService httpClientService;
	
	@Autowired
	private RedisService redisService;
	
	
	@Value("${MANAGE_TAOTAO_URL}")
	private String MANAGE_TAOTAO_URL;
	
	@Value("${TAOTAO_ITEM}")
	private String TAOTAO_ITEM;
	
	@Value("${TAOTAO_ITEM_CACHE_KEY}")
	private String TAOTAO_ITEM_CACHE_KEY;
	
	@Value("${TAOTAO_ITEM_CACHE_SECONDS}")
	private Long TAOTAO_ITEM_CACHE_SECONDS;


	public ItemVO showItem(Long id) {
		//先从缓存中取数据
		String cacheKey = TAOTAO_ITEM_CACHE_KEY + id;
		try {
			
			ItemVO itemVO = redisService.get(cacheKey, new TypeReference<ItemVO>() {
			});
			if (itemVO != null) {
				return itemVO;
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		ItemVO itemVO = httpClientService.doGet(MANAGE_TAOTAO_URL+TAOTAO_ITEM+id, new TypeReference<ItemVO>() {
		});
		
		//将数据放入缓存
		try {
			redisService.set(cacheKey, itemVO,TAOTAO_ITEM_CACHE_SECONDS);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return itemVO;
	}
	
	

}
